  Page({
  data:{
    showList:[],
    goodsList:[],
    goodsIsSelect:[],
    indexArr:[],
    selectArr:[],
    isSelectAll:true,
    isTitleSticky:false,
    isEditor:false,
    token:"",
    stateindex:0,
    amount:0,
    isOk:""
  },
  onPageScroll(e){
    if(e.scrollTop>=20){
      this.setData({
        isTitleSticky:true
      })
    }else{
      this.setData({
        isTitleSticky:false
      })
    }
  },
  //显示删除按钮
  editorOrPay(){
    if(!this.data.isEditor){
      this.setData({
        isEditor:true
      })
    }else if(this.data.isEditor){
      this.setData({
        isEditor:false
      })
    }
  },
  topay(){
    const {indexArr} = this.data
    console.log(indexArr)
    if(indexArr.length==0){
      wx.showToast({
        title: '请选择一种商品',
        icon:"none",
        duration: 2000
      })
      return
    }
    wx.navigateTo({
      url:"/pages/orderform/orderform?indexArr="+JSON.stringify(indexArr)
    })
  },
  handleminus(e){
    let index = e.currentTarget.dataset.index
    if(this.data.goodsList[index].num==1)return
    this.data.goodsList[index].num--
    let newarr = this.data.goodsList
    this.setData({
      goodsList:newarr
    })
    let oneamount = this.data.goodsList[index].num*this.data.goodsList[index].price
    this.countAmount()
    wx.request({
      url:"http://api_devs.wanxikeji.cn/api/shoppingCarAddModify",
      method:"POST",
      data:{
        token:this.data.token,
        good_id:this.data.goodsList[index].good_id,
        num:this.data.goodsList[index].num,
        price:this.data.goodsList[index].price,
        money:oneamount,
        sku:this.data.goodsList[index].sku,
        shopping_car_id:this.data.goodsList[index].shopping_car_id,
      }
    })
  },
  handleadd(e){
    let index = e.currentTarget.dataset.index
    this.data.goodsList[index].num++
    let newarr = this.data.goodsList
    this.setData({
      goodsList:newarr
    })
    let oneamount = this.data.goodsList[index].num*this.data.goodsList[index].price
    this.countAmount()
    wx.request({
      url:"http://api_devs.wanxikeji.cn/api/shoppingCarAddModify",
      method:"POST",
      data:{
        token:this.data.token,
        good_id:this.data.goodsList[index].good_id,
        num:this.data.goodsList[index].num,
        price:this.data.goodsList[index].price,
        money:oneamount,
        sku:this.data.goodsList[index].sku,
        shopping_car_id:this.data.goodsList[index].shopping_car_id,
      }
    })
  },
  getToken(){
    this.data.goodsIsSelect=[]
    wx.getStorage({
      key:"token" ,
      success:(res)=>{
        this.data.token=res.data 
        if(this.data.token=="") return
        this.search()
        wx.request({
          url:"http://api_devs.wanxikeji.cn/api/shoppingCarList",
          method:"POST",
          data:{
            token:this.data.token
          },
          success:(res)=>{
            //初始状态 全部选中
            if(res.data.data.data.length==0){
              this.setData({
                goodsList:[],
                goodsIsSelect:[],
                isEditor:false
              })
              this.countIsSelectAll()
              this.countAmount()
              return
            }
            for(let i=0;i<res.data.data.data.length;i++){
              this.data.goodsIsSelect.push(true)
            }
            this.setData({
              goodsList:res.data.data.data,
              goodsIsSelect:this.data.goodsIsSelect
            })
            this.countIsSelectAll()
            this.countAmount()
          }
        })
      }
    })
  },
  onLoad(option){
    if(option.state){
      this.setDate({
        isOk:option.state
      })
    }
    console.log('我执行了')
    this.setData({
      indexArr:[],
      selectArr:[]
    })
    this.getToken()
  },
  onShow(){
    console.log('我也执行了')
    this.data.stateindex++
    if(this.data.stateindex>1){
      this.getCart()
    }
  },
  getCart(){
    wx.getStorage({
      key:"token" ,
      success:(res)=>{
        this.data.token=res.data 
        if(this.data.token=="") return
        wx.request({
          url:"http://api_devs.wanxikeji.cn/api/shoppingCarList",
          method:"POST",
          data:{
            token:this.data.token
          },
          success:(res)=>{
            if(res.data.data.data.length==0){
              this.setData({
                goodsList:[],
                goodsIsSelect:[],
                isEditor:false
              })
              this.countIsSelectAll()
              this.countAmount()
              return
            }
            this.data.goodsIsSelect=[]
            // console.log(this.data.goodsIsSelect)
            // if(this.data.goodsIsSelect.length==0){
              for(let i=0;i<res.data.data.data.length;i++){
                this.data.goodsIsSelect.push(true)
              }
              // this.setData({
                // goodsIsSelect:this.data.goodsIsSelect,
              // })
            // }
            this.setData({
              goodsList:res.data.data.data,
              goodsIsSelect:this.data.goodsIsSelect
            })
            this.countAmount()
            this.countIsSelectAll()
          }
        })
      }
    })
    
  },
  //计算总价
  countAmount(){
    if(this.data.goodsList==undefined) return
    let {goodsIsSelect,goodsList,indexArr,selectArr} = this.data
    indexArr=[]//储存选中商品的索引的数组
    selectArr=[]//储存选中商品的数组
    for(let i=0;i<goodsIsSelect.length;i++){
      if(goodsIsSelect[i]){
        indexArr.push(i)
      }
    }
    for(let i=0;i<indexArr.length;i++){
      selectArr.push(goodsList[indexArr[i]])
    }
    // console.log(selectArr)
    let result = selectArr.reduce((pre,next)=>{
      return pre+next.price*next.num
    },0)
    console.log(indexArr,selectArr)
    this.setData({
      indexArr,
      selectArr
    })
    if(result==0){
      this.setData({
        amount:result
      })
    }else{
      this.setData({
        amount:result.toFixed(2)
      })
    }
  },
  //计算全选按钮 是否高亮
  countIsSelectAll(){
    let result = this.data.goodsIsSelect.every(item=>item==true)
    this.setData({
      isSelectAll:result
    })
  },
  //删除
  del(e){
    let index = e.currentTarget.dataset.index
    wx.request({
      url:"http://api_devs.wanxikeji.cn/api/shoppingCarDelete",
      method:"POST",
      data:{
        token:this.data.token,
        shopping_car_id:this.data.goodsList[index].shopping_car_id
      },
      success:()=>{
        this.data.goodsIsSelect.splice(index,1)
        this.setData({
          goodsIsSelect:this.data.goodsIsSelect
        })
        this.getCart()
        this.countAmount()
        this.countIsSelectAll()
      }
    })
  },
  //点击全选按钮
  handleSelectAll(){
    if(this.data.isSelectAll){
      //如果是勾选状态 
      for(let i=0;i<this.data.goodsIsSelect.length;i++){
        this.data.goodsIsSelect[i]=false
      }
      this.data.isSelectAll = false
      this.setData({
        isSelectAll:this.data.isSelectAll,
        goodsIsSelect:this.data.goodsIsSelect
      })
    }else{
      //如果是未勾选状态 
      for(let i=0;i<this.data.goodsIsSelect.length;i++){
        this.data.goodsIsSelect[i]=true
      }
      this.data.isSelectAll = true
      this.setData({
        isSelectAll:this.data.isSelectAll,
        goodsIsSelect:this.data.goodsIsSelect
      })
    }
    this.countAmount()
  },
  //点击单选按钮
  handleSelectItem(e){
    let index = e.currentTarget.dataset.index
    this.data.goodsIsSelect[index] = !this.data.goodsIsSelect[index]
    this.setData({
      goodsIsSelect:this.data.goodsIsSelect
    })
    this.countAmount()
    this.countIsSelectAll()
  },
  //跳转详情
  toDetail(e){
    const {goodid} = e.currentTarget.dataset
    let good_id = this.data.goodsList[goodid].good_id
    // console.log("wqhdiwq")
    wx.navigateTo({
      url: `/pages/commodity/commodity?indexArr=${good_id}`
    })
  },
  //预览商品
  goods(e){
    let index = e.currentTarget.dataset.index;
    console.log(this.data.showList[index].good_id);
    wx.navigateTo({
      url: '../commodity/commodity?indexArr='+ this.data.showList[index].good_id
    })
  },
  search(){
    wx.request({
      url: 'http://api_devs.wanxikeji.cn/api/goodList',
      method:'POST',
      data:{
        page:"1",
        size:"10",
      },
      success:(res)=>{
         this.setData({
          showList:res.data.data.data
        })
      }
    })
  }
})